package com.jrx.anytxn.transaction.job.paph.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.PaphFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author 张靖烽
 * @name MergeServiceListener
 * @description 检查文件
 * @create 2021-03-15 10:36
 **/
@Service
public class MergeServiceListener implements JobExecutionListener {

    private static final Logger logger = LoggerFactory.getLogger(MergeServiceListener.class);

    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private PaphFileConfig paphFileConfig;


    @Override
    public void beforeJob(JobExecution jobExecution) {
        // 当前业务日期
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        String nextProcessingDate = DateFormatUtils.format(org.getNextProcessingDate(), DateUtils.DATE_PATTERN_ANOTHER);
        checkFileAndPath(org.getTenantId(), nextProcessingDate, Constant.PRODECUT_TYPE_XF);
        checkFileAndPath(org.getTenantId(), nextProcessingDate, Constant.PRODECUT_TYPE_JY);
    }

    private void checkFileAndPath(String tenantId, String nextProcessingDate, String product) {
        // 文件路径
        String filePath = paphFileConfig.getServiceFeePath() + tenantId + File.separator + nextProcessingDate + File.separator + product + File.separator;
        Path path = Paths.get(filePath);
        // txt文件
        String txtFileName = Constant.LS_BANK + product + nextProcessingDate + TransBizConstant.SERVICE_FEE_FILE_NAME + ".txt";
        Path txtFilePath = Paths.get(filePath + txtFileName);
        // MD5文件
        String md5FileName = Constant.LS_BANK + product + nextProcessingDate + TransBizConstant.SERVICE_FEE_FILE_NAME + ".md5";
        Path md5FilePath = Paths.get(filePath + md5FileName);
        logger.info("》》》》平安普惠文件合并前检查《《《《path:{},txt:{},md5:{}", filePath, txtFileName, md5FileName);
        //判断文件夹是否存在
        if (!Files.isDirectory(path)) {
            //创建文件夹
            try {
                Files.createDirectories(path);
            } catch (IOException e) {
                logger.error("创建目录失败", e);
            }
        }
        // 文件删除
        if (Files.exists(txtFilePath)) {
            try {
                Files.delete(txtFilePath);
            } catch (IOException e) {
                logger.error("文件{}删除失败", txtFilePath);
            }
        }
        if (Files.exists(md5FilePath)) {
            try {
                Files.delete(md5FilePath);
            } catch (IOException e) {
                logger.error("文件{}删除失败", md5FilePath);
            }
        }
        //创建文件
        try {
            Files.createFile(txtFilePath);
            Files.createFile(md5FilePath);
        } catch (IOException e) {
            logger.error("创建文件失败", e);
        }
    }

    @Override
    public void afterJob(JobExecution jobExecution) {

    }
}
